package com.treasure.mall.biz.repository.live;

import com.treasure.mall.biz.entity.live.LiveDirectorPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/13
 */
@Repository
public interface LiveDirectorRepository extends JpaRepository<LiveDirectorPO, Long> {
    List<LiveDirectorPO> findByRoomIdAndDeletedIsFalse(Long roomId);

    LiveDirectorPO findByRoomIdAndUserPO_UserId(Long roomId, Long userId);


    /**
     * 获取用户授权的主播间
     *
     * @param userId
     * @return
     */
    @Query("select po from LiveDirectorPO  po where po.userPO.userId=?1 and po.owner=false and po.deleted = false")
    List<LiveDirectorPO> findByUserId(Long userId);


    /**
     * 获取收取的主播间数量
     *
     * @param userId
     * @return
     */
    long countByUserPO_UserIdAndOwnerIsFalseAndDeletedIsFalse(Long userId);

    /**
     * 删除主播
     *
     * @param id
     */
    @Query("update LiveDirectorPO po set po.deleted=true where po.id=?1 ")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void delete(Long id);
}
